/*================================================================
*   Copyright (C) 2020 belonging to YangZhengQing. All rights reserved.
*   
*   文件名称：tlv_unpack.c
*   创 建 者：YangZhengQing
*   创建日期：2020年03月03日
*   描    述：
*
*================================================================*/


#include "../include/server.h"
#include "../include/type.h"
/*
int main(int argc,char **argv)
{
     char buff[58] = {0xfd,0xfd,0x1 ,0xA ,0x32 ,0x30 ,0x32 ,0x30 ,0x2d ,0x30 ,0x34,\
                    0x2d ,0x30 ,0x36 ,0x2 ,0x8 ,0x31 ,0x32 ,0x3a ,0x30 ,0x35 ,0x3a,\
                    0x31 ,0x37 ,0x3 ,0xF ,0x32 ,0x38 ,0x2d ,0x30 ,0x34 ,0x31 ,0x37,\
                    0x33 ,0x31 ,0x66 ,0x37 ,0x63 ,0x30 ,0x66 ,0x66 ,0x4 ,0x9 ,0x31,\
                    0x35 ,0x2e ,0x30 ,0x36 ,0x33 ,0xe2 ,0x84 ,0x83 ,0x05 ,0x01 ,0xb6 ,0xdf ,0xdf};

    int rv = -1;
    pack_t parse_pack;
    //清空结构体
    memset(parse_pack.date,0,sizeof(parse_pack.date));
    memset(parse_pack.time,0,sizeof(parse_pack.time));
    memset(parse_pack.ID,0,sizeof(parse_pack.ID));
    memset(parse_pack.temp,0,sizeof(parse_pack.temp));

    rv = tlv_unpack(buff,57,&parse_pack);

    printf("date:%s\n",parse_pack.date);
    printf("time:%s\n",parse_pack.time);
    printf("ID:%s\n",parse_pack.ID);
    printf("temp:%s\n",parse_pack.temp);
}
*/


/*
 功能：数据包解包
 参数：
	data:待解包的数据报文
    size:数据包的大小
	pp:解析之后数据缓冲区
 返回：成功返回0，失败返回-1

 */

int tlv_unpack(u8 *data, int size, pack_t *pp)
{

    int                      rv = -1;
    u8                     *ptr ;
    unsigned char            len;
    unsigned char            i;//不能使用char型做数组下标

    //校验数据包
    rv = pack_crc(data);
    if(rv < 0)
    {
        printf("CRC vrified failly!\n");
        return -1;
    }

    printf("CRC vrified successfully! Will start unpacking!\n");
    ptr = data;
    while(*ptr != TAG_CRC)
    {
        if(*ptr == TAG_DATE)
        {
            ptr += 1;
            len = *ptr;
            for(i = 0; i < len;i++)
            {
                ptr++;
                pp->date[i] = *ptr;
            }
        }
        else if(*ptr == TAG_TIME)
        {
            ptr++;
            len = *ptr;
            for(i = 0; i < len; i++)
            {
                ptr++;
                pp->time[i] = *ptr;
            }
        }
        else if(*ptr == TAG_DEVICE)
        {
            ptr++;
            len = *ptr;
            for(i = 0; i < len;i++)
            {
                ptr++;
                pp->ID[i] = *ptr;
            }
        }
        else if(*ptr == TAG_TEMP)
        {
            ptr++;
            len = *ptr;
            for(i = 0; i < len;i++)
            {
                ptr++;
                pp->temp[i] = *ptr;
            }
        }

        ptr++;

    }

    return 0;
}


/* 功能：CRC检验数据包
 * 参数：data:数据包首地址
 * 返回值：成功返回0，失败返回-1*/
int pack_crc(u8 *data)
{
    u8              *crc_start;
    u8            *crc_end;
    u8              crcval;
    CRC_8           type;
    
    type.poly       = POLYNOMIAL;//多项式
    type.InitValue  = INITVAL;//初始值
    type.xor        = XOR;//异或值
    type.InputReverse = TRUE;
    type.OutputReverse= TRUE;

    crc_end = data;

    while(*crc_end != TAG_CRC)
    {
        if(*crc_end == TAG_DATE)
        {
            crc_start = crc_end;
        }
        crc_end++;
    }

    crcval = crc8(crc_start, (crc_end - crc_start), type);
    crc_end += 2;
    if(crcval != *crc_end)
    {
        return -1;
    }

    return 0;
}

/*功能：结构体清零
 * 参数：pp:结构体
 * 返回值:无*/

void memset_pack_struct(pack_t *pp)
{
    memset(pp->date,0,sizeof(pp->date));
    memset(pp->time,0,sizeof(pp->time));
    memset(pp->ID,0,sizeof(pp->ID));
    memset(pp->temp,0,sizeof(pp->temp));

}


/*
  功能：计算出数据报文的总长度
  参数：
	data:数据报文
  返回：数据报文长度
 */
int pack_length(unsigned char *data)
{
	int				length = 0;

	while(data[length] != REAR)
	{
		length++;
	}

	return length;
}


